//
//  MBProgressHUD+Extension.swift
//  Simple Switf Project
//
//  Created by Chen on 2017/6/7.
//  Copyright © 2017年 陈卫东. All rights reserved.
//

import Foundation
import MBProgressHUD

extension MBProgressHUD {
    class func show(message msg: String, icon: String?, view: UIView?, afterDelay: TimeInterval) {
        var view = view
        if view == nil {
            view = UIApplication.shared.windows.last
        }
        
        let hud = MBProgressHUD.showAdded(to: view!, animated: true)
        hud.detailsLabel.text = msg
        hud.detailsLabel.font = Font(font: 14)
        hud.mode = .customView
        hud.contentColor = UIColor.white
        hud.bezelView.style = .solidColor
        hud.bezelView.color = UIColor.black.withAlphaComponent(0.8)
        hud.customView = UIImageView(image: UIImage(named: icon!))
        hud.hide(animated: true, afterDelay: afterDelay)
    }
    
// MARK: 显示失败信息
    public class func showError(message msg: String, view: UIView?) {
        self.showError(message: msg, view: view) {
            
        }
    }
    
    public class func showError(message msg: String, view: UIView?, complete: () -> ()) {
        let characterCount = Double(msg.characters.count)
        let timeInterval: TimeInterval = characterCount * 0.15 > 2 ? 2 : characterCount * 0.15
        self.show(message: msg, icon: "jb_hud_error", view: view, afterDelay: timeInterval)
    }
    
// MARK: 显示成功信息
    public class func showSuccess(message msg: String, view: UIView?) {
        self.showSuccess(message: msg, view: view) { 
            
        }
    }
    
    public class func showSuccess(message msg: String, view: UIView?, complete: () -> ()) {
        let characterCount = Double(msg.characters.count)
        let timeInterval: TimeInterval = characterCount * 0.15 > 2 ? 2 : characterCount * 0.15
        self.show(message: msg, icon: "jb_hud_success", view: view, afterDelay: timeInterval)
    }
    
// MARK: 显示一些信息
    public class func showMessage(msg: String, view: UIView?) {
        var view = view
        if view == nil {
            view = UIApplication.shared.windows.last
        }
        
        let hud = MBProgressHUD.showAdded(to: view!, animated: true)
        hud.detailsLabel.text = msg
        hud.detailsLabel.font = Font(font: 14)
        hud.contentColor = UIColor.white
        hud.bezelView.style = .solidColor
        hud.bezelView.color = UIColor.black.withAlphaComponent(0.8)
    }
    
    public class func hideHUD(toView view: UIView) {
        self.hide(for: view , animated: true)
    }
}
